package com.david.webtest.blog

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.david.webtest.WebApplication
import com.david.webtest.R
import com.david.webtest.utils.BlogUtils
import java.util.*


//TODO 添加新增，删除，分享
class BlogListActivity :AppCompatActivity(), BlogAdapter.OnBlogItemClickListener {

    private var urlList: ArrayList<BlogBean> = ArrayList()

    private lateinit var adapter:BlogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blog_list)


        val recycler:RecyclerView = findViewById(R.id.blog_list)
        recycler.layoutManager = LinearLayoutManager(this)

        adapter = BlogAdapter()
        recycler.adapter =adapter
        val dec = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        getDrawable(R.drawable.shape_blog_divider)?.let { dec.setDrawable(it) }

        recycler.addItemDecoration(dec)


        adapter.onBlogItemClickListener = this

        BlogUtils.getBlog(object : BlogUtils.OnBlogRequestResponse() {
            override fun onBlogRequested(result: ArrayList<BlogBean>) {
                adapter.list = result
                Toast.makeText(WebApplication.instance, "onBlogRequested " + result[0].name, Toast.LENGTH_LONG).show()
            }

            override fun onBlogGetFail(msg: String) {
                Toast.makeText(WebApplication.instance, msg, Toast.LENGTH_LONG).show()
            }
        })

        findViewById<View>(R.id.blog_list_add).setOnClickListener {

        }

    }

    override fun onBlogItemClick(url: String) {
        val intent = Intent(this, BlogWebActivity::class.java)
        intent.putExtra(BlogWebActivity.INTENT_WEB_URL, url)
        startActivity(intent)
    }


}